<?php
class Person {
	static public function get($db="",$id=-1,$where="",$field="*") {
		if($db=="")$db=DB::$db;
		$rows = DB::query("select ".$field." from person".($id!=-1?" where id='" . $id . "'":"").($where!=""&&$id==-1?" where ".$where:""));
		if($id!=-1) {
			$p = $rows[0];
			if(isset($p['id']))$p['users'] = User::getCollection($p['id'], $db);
			return $p;
		}else if(count($rows)>0){
			return $rows;
		}
	}
	
	static function getFio($person){
		if(isset($person["surname"])&&isset($person["name"]))
		return $person["surname"]." ".mb_substr($person["name"], 0,1,"UTF-8").".".(isset($person["patronymic"])?mb_substr($person["patronymic"], 0,1,"UTF-8").".":"");
	}
	
	static function validate($res=array("state"=>true,"error"=>array()), $db) {
		$p = $_POST['person'];
		if(!isset($p)){
			$res['state'] = false;
			return $res;
		}
// check name
		if(!(isset($p['name']) && !empty($p['name']) && strlen(utf8_decode($p['name'])) > 2 && strlen(utf8_decode($p['name'])) < 100 && preg_match("/[А-Я, а-я, A-Z, a-z]/", $p['name']))) {
			$res['state'] = false;
			$res['error']['person[name]'] = 1;
		}
// check patronymic
		if (isset($p['patronymic']) && !empty($p['patronymic']) && !(strlen(utf8_decode($p['patronymic'])) > 2 && strlen(utf8_decode($p['patronymic'])) < 100)) {
			$res['state'] = false;
			$res['error']['person[patronymic]'] = 1;
		}
// check surname
		if (isset($p['surname']) && !empty($p['surname']) && !(strlen(utf8_decode($p['surname'])) > 2 && strlen(utf8_decode($p['surname'])) < 100)) {
			$res['state'] = false;
			$res['error']['person[surname]'] = 1;
		}
// check post
		if (isset($p['post']) && !empty($p['post']) && !(utf8_decode(strlen($p['post'])) > 2 && strlen(utf8_decode($p['post'])) < 255)) {
			$res['state'] = false;
			$res['error']['person[post]'] = 1;
		}
// check organization
		if (isset($p['organization']) && !empty($p['organization']) && !(strlen(utf8_decode($p['organization'])) > 2 && strlen(utf8_decode($p['organization'])) < 255)) {
			$res['state'] = false;
			$res['error']['person[organization]'] = 1;
		}
// check degree
		if (isset($p['degree']) && !empty($p['degree']) && !(strlen(utf8_decode($p['degree'])) > 2 && strlen(utf8_decode($p['degree'])) < 255)) {
			$res['state'] = false;
			$res['error']['person[degree]'] = 1;
		}						
// check country
		if (isset($p['country']) && !empty($p['country']) && is_int((int)$p['country'])) {
			$country = Country::get($p['country']);
			if(count($country)!=1){
				$res['state'] = false;
				$res['error']['person[country]'] = 1;
			}
		}else{
			$res['state'] = false;
			$res['error']['person[country]'] = 1;
		}
// check phone
		if (isset($p['phone']) && !empty($p['phone']) && !(strlen(utf8_decode($p['phone'])) > 2 && strlen(utf8_decode($p['phone'])) < 100)) {
			$res['state'] = false;
			$res['error']['person[phone]'] = 1;
		}
// check city
		if (!(isset($p['city']) && !empty($p['city']) && strlen(utf8_decode($p['city'])) > 2 && strlen(utf8_decode($p['city'])) < 100)) {
			$res['state'] = false;
			$res['error']['person[city]'] = 1;
		}							
// check address
		if (isset($p['address']) && !empty($p['address'])&&!(strlen(utf8_decode($p['address'])) > 2 && strlen(utf8_decode($p['address'])) < 255)) {
			$res['state'] = false;
			$res['error']['person[address]'] = 1;
		}
// check email
		if (!(isset($p['email']) && !empty($p['email']) && strlen(utf8_decode($p['email'])) > 2 && strlen(utf8_decode($p['email'])) < 255)) {
			$res['state'] = false;
			$res['error']['person[email]'] = 1;
		}else{
			$a = $db->query("SELECT email FROM `person` WHERE email='".$p['email']."'")->fetchAll();
			if($res["action"] == "register"){
				if(!empty($a)){
					$res['state'] = false;
					$res['error']['person[email]'] = 1;
				}
			}
		}
		return $res;
	}

	static function create($db){
		$data['state'] 	= true;
		$p 				= $_POST['person'];
		$sql 			= "INSERT INTO person (";
		$values 		= "";
		$comma 			= "";
		foreach($p as $key => $value){
			$values.=$comma."'".trim($value)."'";
			$sql.=$comma."`".$key."`";
			$comma = ",";
		}
		try{
			$sql = $sql.") values (".$values.")";
			$dbres = DB::exec_($sql);
		}catch(PDOException $e){
			$data['msg'] ="Error: ".$e->getMessage()." sql: ".$sql;
			$data['state'] = false;
		}
		$id = DB::query("SELECT id FROM `person` WHERE email='".$p['email']."'");
		$data['id'] = $id[0]['id'];
		return $data;
	} 
	
	//set new settings of person
	static function set($data, $db=""){
		if($db=="")$db=DB::$db;
		$res['state'] 	= true;
		$p 				= $data;
		$sql 			= "update person set ";
		$comma 			= "";
		foreach($p as $key => $value){
			$val 								= trim($value);
			$_SESSION['user']['person'][$key] 	= $val;
			$sql.=$comma."`".$key."`='".$val."'";
			$comma=",";
		}
		try{
			$db->exec($sql." where `id`='".$_SESSION['user']['person']['id']."'");
		}catch(PDOException $e){
			$res['msg'] 	= "Error: ".$e->getMessage()." sql: ".$sql;
			$res['state'] 	= false;
		}
		return $res;
	}

}
?>